﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Inpinke.BLL.PDFProcess;
using Inpinke.BLL.Filters;
using Inpinke.BLL;
using Inpinke.Model;
using Inpinke.Model.Enum;
using Inpinke.BLL.Session;
using Inpinke.Model.CustomClass;
using Inpinke.BLL.Config;

namespace inpinke.com.Controllers
{
    public class InWayController : Controller
    {
        //
        // GET: /InWay/

        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// Intime编辑器
        /// </summary>
        /// <returns></returns>
        [UserFilter]
        public ActionResult Editor(int? bookid)
        {
            if (!bookid.HasValue)
            { 
                ViewBag.Msg = "访问出错，链接地址不正确。";
                return View("error"); 
            }
            Inpinke_Book model = DBBookBLL.GetBookByID(bookid.Value);
            if (model != null)
            {
                if (model.BookStauts == (int)BookStatus.Making)
                {
                    ViewBag.Msg = "印品已下单印刷不能再做修改，您可以拷贝副本进行编辑/联系客服寻求帮助";
                    return View("error");
                }
                if (model.UserID != UserSession.CurrentUser.ID)
                {
                    ViewBag.Msg = "对不起，您不能编辑该印品，因为那件印品好像不属于您。";
                    return View("error");
                }
                ViewBag.EditBook = model;
            }
            else
            {
                ViewBag.Msg = "对不起，没有找到您要编辑的印品。";
                return View("error");
            }
            return View();
        }

        /// <summary>
        /// 添加一本新书
        /// </summary>
        /// <returns></returns>
        [UserFilter]
        public ActionResult CreateInway(int prodid)
        {
            Inpinke_Product inway = DBProductBLL.GetProductByID(prodid);
            Inpinke_Book model = new Inpinke_Book()
            {
                UserID = UserSession.CurrentUser.ID,
                BookName = "我的旅途",
                Author = UserSession.CurrentUser.NickName,
                BookDesc = ConfigMap.GetBookDesc(inway.ProductCode),
                PageCount = inway.BasePages,
                ProductID = prodid,
                ShowStatus = (int)ShowStatus.Public,
                BookCover = "/Content/pagestyle/images/inway_cover.png"
            };
            BaseResponse br = DBBookBLL.AddBook(model);
            if (br.IsSuccess)
            {
                DBBookBLL.CopyBookPageStyle(model.ID, ConfigMap.GetStyleBookID(model.Inpinke_Product.ProductCode));
                return RedirectToAction("editor", new { bookid = model.ID });
            }
            else
            {
                ViewBag.Msg = "对不起，定制印品失败，您可以重新登录再尝试下。";
                return View("error");
            }
        }

        /// <summary>
        /// 生成Intime书本的PDF文件
        /// </summary>
        /// <param name="bookid"></param>
        /// <returns></returns>
        public ActionResult CreateInwayPDF(int bookid)
        {
            InWayPDFBLL.CreateBookPDF(bookid);
            return Content(true.ToString());
        }

    }
}
